<?php
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$opUI = Blyn::app()->getActiveOperation()->getUIManager();
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$url = $opUI->getAjaxLinkUrl('saveMessage', NULL, array(
    'messageId' => $poolsId,
    'parentEventId' => $eventId,
    'postModel' => $modelClass));
$updateId = $opUI->getAjaxContainerId();
$inputTypeList = BWInputType::getTypeList();

$messageNameField = 'message_name_' . $poolsId;
$imageField = 'image_media_image_' . $poolsId;
$myPoolsField = 'inputMedia_myPools_' . $poolsId;
$myPoolsInputTypeField = 'inputType_myPools_' . $poolsId;
//$poolsQuestionField = 'media_question_' . $poolsId;
$poolsAnswer1Field = 'inputable_myPools_answer1_' . $poolsId;
$poolsAnswer2Field = 'inputable_myPools_answer2_' . $poolsId;

$model->addField($messageNameField);
$model->addField($imageField);
$model->addField($myPoolsField);
$model->addField($myPoolsInputTypeField);
//$model->addField($poolsQuestionField);
$model->addField($poolsAnswer1Field);
$model->addField($poolsAnswer2Field);


if ($poolsId > 0) {
    $poolsMessage = new BWMessage($poolsId);
    $model->$messageNameField = $poolsMessage->name;

    $imageMedia = $poolsMessage->getMediaManager()->getMedia('image');
    if ($imageMedia) {
        $model->$imageField = $poolsMessage->getMediaManager()->getMedia('image')->content;
    }

    $myPools = $poolsMessage->getInputMediaManager()->getInputMedia('myPools');
    if ($myPools instanceof BWInputMedia) {
        $myPoolsInputType = $myPools->getInputType();
        $model->$myPoolsField = $myPools->content;
        $model->$myPoolsInputTypeField = get_class($myPoolsInputType);
        $model->$poolsAnswer1Field = $myPoolsInputType->getInputableByName('answer1');
        $model->$poolsAnswer2Field = $myPoolsInputType->getInputableByName('answer2');
    }
}
?>
<?php
//$screen = Blyn::app()->getUIManager()->getCurrentScreen();
//$url = $screen->getAjaxLinkUrl('saveMessage', NULL, array('messageId' => $brandId,'parentEventId' => $eventId));
//$updateId = $screen->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send<?php echo $poolsId; ?>() {

//        var formData = new FormData($("#message-form")[0]);
        var formElement = document.getElementById("brand-form");
        var formData = new FormData(formElement);

        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").load("<?php echo $opUI->getLayoutView(); ?>");
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>

<div id="myModal<?php echo $poolsId; ?>" class="modal hide fade" style="width: 1000px;left: 33%; " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3>投票活动</h3>
    </div>
    <div class="modal-body" id="PinPaiYouhui" style="display:block">

        <table class="table table-bordered table-striped">
            <colgroup>
                <col class="span1">
                <col class="span7">
            </colgroup>
            <thead>
                <tr>
                    <th>属性</th>
                    <th>内容</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td> <code>投票活动名称</code> </td>
                    <td><?php echo $form->textArea($model, $messageNameField, array('placeholder' => '名称...', 'required' => true)); ?></td>
                </tr>
                <tr>
                    <td> <code>问题</code> </td>
                    <td><?php echo $form->textArea($model, $myPoolsField, array('placeholder' => '问题...', 'required' => true)); ?></td>
                </tr>
                <tr>
                    <td> <code>活动的数据类型</code> </td>
                    <td><?php echo $form->dropDownList($model, $myPoolsInputTypeField, $inputTypeList, array('class' => 'span7', 'required' => true)); ?></td>
                </tr>
                <tr>
                    <td> <code>可选答案1</code> </td>
                    <td><?php echo $form->textArea($model, $poolsAnswer1Field, array('placeholder' => '可选答案1...', 'required' => true)); ?></td>
                </tr>
                <tr>
                    <td> <code>可选答案2</code> </td>
                    <td><?php echo $form->textArea($model, $poolsAnswer2Field, array('placeholder' => '可选答案2...')); ?></td>
                </tr>

                <tr><td><code>投票图片</code> </td>
                    <td>
                        <?php
                        echo $form->fileField($model, $imageField);
                        if ($poolsId > 0) {
                            $imageUrl = Yii::app()->request->baseUrl . $model->$imageField;
                            echo "<img src=" . $imageUrl . " width=50 height=50 />";
                        }
                        ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div> 
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
        <button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:send<?php echo $poolsId; ?>()">保存</button>

    </div>
</div>
